package com.treasure.mall.zbqmallapi.controller;

import com.treasure.mall.biz.entity.product.CategoryPO;
import com.treasure.mall.biz.mapstruct.CategoryMapStrcut;
import com.treasure.mall.biz.service.core.product.CategoryService;
import com.treasure.mall.biz.vo.product.CategoryTreeVo;
import com.treasure.mall.biz.vo.product.CategoryVo;
import com.treasure.mall.zbq.common.base.ApiResult;
import com.treasure.mall.zbq.common.base.ResultCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wm
 */
@Slf4j
@RequestMapping("/categories")
@RestController
@Api(tags = "类目模块")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @ApiOperation("获取类目树")
    @PostMapping("/tree")
    public ApiResult getTree() {
        return ApiResult.ok(categoryService.getApiTree());
    }

    /**
     * 获取下级类目数据
     *
     * @param cateId
     * @return
     */
    @ApiOperation("获取下级类目数据")
    @GetMapping("/{id}/children")
    public ApiResult children(@PathVariable("id") Long cateId) {
        List<CategoryPO> list = categoryService.getList(cateId);
        return ApiResult.ok(CategoryMapStrcut.INSTANCE.po2vos(list));
    }

    /**
     * 获取上级类目信息
     *
     * @param parentId
     * @return
     */
    @ApiOperation("获取上级类目信息")
    @GetMapping("/{id}/parent")
    public ApiResult parent(@PathVariable("id") Long parentId) {
        CategoryPO po = categoryService.getSingle(parentId);
        if (po != null) {
            return ApiResult.ok(CategoryMapStrcut.INSTANCE.po2vo(po));
        }
        return ApiResult.error("类目不存在");
    }

}